package service

import (
	"core/db"
	"core/entity"
)

func ListNamespaces() (namespaces []entity.Namespace, err error) {
	result := db.DB.Find(&namespaces)
	if result.Error != nil {
		return nil, result.Error
	}
	return namespaces, nil
}

func FindNamespaceByName(name string) (namespace *entity.Namespace, err error) {
	namespace = new(entity.Namespace)
	result := db.DB.Where("name = ?", name).Find(namespace)
	if result.Error != nil {
		return nil, result.Error
	}
	return namespace, nil
}

func FindNamespaceKeyByName(name string) (namespace []string, err error) {
	pairs := make([]entity.KVPair, 0)
	result := db.DB.Where("namespace_name = ?", name).Find(&pairs)
	if result.Error != nil {
		return nil, result.Error
	}

	var keys []string

	for _, pair := range pairs {
		keys = append(keys, pair.KVKey)
	}
	return keys, nil
}
